python subprocess call

您所在的位置:网站首页 python subprocesscall python subprocess call

python subprocess call

#python subprocess call| 来源: 网络整理| 查看: 265

Introduction

The subprocess module is used to create a new process, connect to its stdin, stdout, stderr pipes and get their return codes. The emergence of the subprocess module is to replace the following old modules and functions: os.system, os.spawn*, os.popen*, popen2.*, commands.*. It is strongly recommended that POSIX users (Linux, BSD, etc.) install and use the newer subprocess32 module instead of the subprocess that comes with Python 2.7.

shortcut function

It is recommended that users use the three shortcut functions call, check_call, and check_output, and only use the more advanced Popen interface when the requirements cannot be met.

call

subprocess. call(args, *, stdin = None, stdout = None, stderr = None, shell = False)

Run the command provided by the args parameter, wait for the command to complete and return the return code. When the args parameter is provided in the form of a string and there are multiple command parameters, the shell=True parameter needs to be provided:

res = subprocess. call('ls')

print 'res:', res

or:

res = subprocess. call('ls -l', shell = True)

print 'res:', res

When multiple command parameters are provided in the form of a list, it is not necessary to provide the shell=True parameter:

res = subprocess. call(['ls', '-l'])

print 'res:', res

Be careful not to assign subprocess.PIPE to the stdout and stderr parameters. If the subprocess output is large, it will cause deadlock. These two parameters can be assigned to subprocess.STDOUT to print to the screen or assigned to a file object to write the output to a file:

//test.py

import subprocess as sp

sp.call('python run.py', shell = True, stdin=open('fake_input', 'r'), stdout=open('result', 'w'))

//run.py

i = int(raw_input("Input a number:"))

print "You input number:", i

After running test.py, the content in result is:

Input a number: You input a number: 12

check_call

subprocess. check_call(args, *, stdin = None, stdout = None, stderr = None, shell = False)

Similar to the call method, the difference is that if the command line is successfully executed, check_call returns a return code of 0, otherwise a subprocess.CalledProcessError exception is thrown.

subprocess.CalledProcessError exception includes returncode, cmd, output and other attributes, where returncode is the exit code of the subprocess, cmd is the execution command of the subprocess, and output is None.

import subprocess

try:

res = subprocess. check_call(['ls', '('])

print 'res:', res

except subprocess.CalledProcessError, exc:

print 'returncode:', exc.returncode

print 'cmd:', exc.cmd

print 'output:', exc. output

Results of the:

ls: (: No such file or directory

returncode: 1

cmd: ['ls', '(']

output: None

Note: Do not assign subprocess.PIPE to the stdout and stderr parameters.

check_output

subprocess. check_output(args, *, stdin = None, stderr = None, shell = False, universal_newlines = False)

Execute the command in the child process, and return the output of the execution result in the form of a string. If the subprocess exit code is not 0, a subprocess.CalledProcessError exception is thrown, and the output field of the exception contains the error output:

import subprocess

try:

res = subprocess. check_output('ls xxx',

stderr = subprocess. STDOUT,

shell=True)

print 'res:', res

except subprocess.CalledProcessError, exc:

print 'returncode:', exc.returncode

print 'cmd:', exc.cmd

print 'output:', exc. output

Results of the:

returncode: 1

cmd: ls xxx

output: ls: xxx: No such file or directory

Note: Do not assign subprocess.PIPE to the stderr parameter.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3